/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.base;

import cn.ac.iie.di.config.ConfigureUtil;
import cn.ac.iie.di.Reader.Reader;
import cn.ac.iie.di.writer.Writer;
import java.io.File;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.Logger;
/**
 *
 * @author LXH
 */
public class Startup{
    public static volatile boolean stop;
    private static final Logger LOG = Logger.getLogger(Startup.class.getName());
    public static  BlockingQueue<String> queue;
    public static ExecutorService executorService = Executors.newFixedThreadPool(10);
    public static ConfigureUtil config;
    
    
    public static void main(String arges[]) throws InterruptedException {
        //读取配置文件
        if (arges.length == 0) {
            config = ConfigureUtil.getInstanse(Startup.class.getClassLoader().getResource("longchun.properties").getPath());
            PropertyConfigurator.configure(Startup.class.getClassLoader().getResource("log4j.properties").getPath());
        } else {
            PropertyConfigurator.configure(arges[0]+ File.separator + "log4j.properties");
            config = ConfigureUtil.getInstanse(arges[0]+ File.separator + "longchun.properties");
        }
        
        //给队列赋予长度
        queue= new ArrayBlockingQueue<>(config.getInt("queue", "100"));
        LOG.info("queue "+config.getInt("queue", "100"));//
        
        //能接受到关闭信息把所有循环的条件 ！stop变为false
        KillHandler killHandler = new KillHandler();
        killHandler.registerSignal("TERM");
        
        //启动单线程循环读取原混合文件中需要读取的
        executorService.execute(new Reader());
        
        //启动线程写入
        for(int i=0;i<config.getInt("threadnum","1");i++){
            executorService.execute(new Writer());
        }
        //主线程表示程序执行
        while(!stop){
            LOG.info("runing........................");
            Thread.sleep(1000l*60);
        }
        //一旦执行到这里证明循环开关已经关闭，程序即将结束
        executorService.shutdown();
        //打印结束日志
        LOG.info("stop....................");
    }
}
